package hivi.vn.hivigold.activities;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.database.Preferences;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LCDSetting extends Activity {

	Button btnLogo;
	Button btnStart;

	EditText txt_companytitle;
	EditText txt_companyname;
	EditText txt_tpricename;
	EditText txt_sologan;
	EditText txt_address;
	EditText txt_phone;
	EditText txt_logo;
	EditText txt_pricedata;
	EditText txt_header1;
	EditText txt_header2;
	EditText txt_header21;
	EditText txt_header22;
	EditText txt_header3;
	EditText txt_wellcome;

	private static int RESULT_LOAD_IMAGE_LOGO = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lcdsetting);

		btnLogo = (Button) findViewById(R.id.btnlogo);
		btnStart = (Button) findViewById(R.id.btnStart);

		txt_companytitle = (EditText) findViewById(R.id.txt_companytitle);
		txt_companyname = (EditText) findViewById(R.id.txt_companyname);
		txt_tpricename = (EditText) findViewById(R.id.txt_tpricename);
		txt_address = (EditText) findViewById(R.id.txt_address);
		txt_sologan = (EditText) findViewById(R.id.txt_sologan);
		txt_phone = (EditText) findViewById(R.id.txt_phone);
		txt_logo = (EditText) findViewById(R.id.txt_logo);
		txt_pricedata = (EditText) findViewById(R.id.txt_pricedata);
		txt_header1 = (EditText) findViewById(R.id.txt_header1);
		txt_header2 = (EditText) findViewById(R.id.txt_header2);
		txt_header21 = (EditText) findViewById(R.id.txt_header21);
		txt_header22 = (EditText) findViewById(R.id.txt_header22);
		txt_header3 = (EditText) findViewById(R.id.txt_header3);
		txt_wellcome = (EditText) findViewById(R.id.txt_wellcome);

		loadConfigData();

		btnLogo.setOnClickListener(onClickListener);
		btnStart.setOnClickListener(onClickListener);
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnlogo:
				Intent ilogo = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(ilogo, RESULT_LOAD_IMAGE_LOGO);
				break;
			case R.id.btnStart:
				if (checkValidData()) {
					saveConfigData();
					LCDPresentation.onStartIntent(getBaseContext());
				}
				break;
			}
		}
	};

	protected Boolean checkValidData() {
		// TODO Auto-generated method stub
		if (txt_pricedata.getText().toString().matches("")) {
			Toast bread = Toast.makeText(getApplicationContext(),
					R.string.lcd_wrnpricedata, Toast.LENGTH_SHORT);
			bread.show();
			return false;
		}
		return true;
	}

	protected void loadConfigData() {
		txt_companytitle.setText(Preferences.getPreferenceValue(this,
				Preferences.COMPANYTITLE));
		txt_companyname.setText(Preferences.getPreferenceValue(this,
				Preferences.COMPANYNAME));
		txt_tpricename.setText(Preferences.getPreferenceValue(this,
				Preferences.TPRICENAME));
		txt_address.setText(Preferences.getPreferenceValue(this,
				Preferences.ADDRESS));
		txt_sologan.setText(Preferences.getPreferenceValue(this,
				Preferences.SOLOGAN));
		txt_phone.setText(Preferences.getPreferenceValue(this,
				Preferences.PHONE));
		txt_logo.setText(Preferences.getPreferenceValue(this, Preferences.LOGO));
		txt_pricedata.setText(Preferences.getPreferenceValue(this,
				Preferences.PRICEDATA));
		txt_header1.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER1));
		txt_header2.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER2));
		txt_header21.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER21));
		txt_header22.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER22));
		txt_header3.setText(Preferences.getPreferenceValue(this,
				Preferences.HEADER3));
		txt_wellcome.setText(Preferences.getPreferenceValue(this,
				Preferences.WELLCOME));

	}

	protected void saveConfigData() {
		Preferences.editPreferenceValue(this, Preferences.COMPANYTITLE,
				txt_companytitle.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.COMPANYNAME,
				txt_companyname.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.TPRICENAME,
				txt_tpricename.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.SOLOGAN, txt_sologan
				.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.ADDRESS, txt_address
				.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.PHONE, txt_phone
				.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.LOGO, txt_logo
				.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.PRICEDATA,
				txt_pricedata.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.HEADER1, txt_header1
				.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.HEADER2, txt_header2
				.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.HEADER21,
				txt_header21.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.HEADER22,
				txt_header22.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.HEADER3, txt_header3
				.getText().toString());
		Preferences.editPreferenceValue(this, Preferences.WELLCOME,
				txt_wellcome.getText().toString());

	}

	public static void onStartIntent(Context c) {
		Intent i = new Intent(c, LCDSetting.class);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		c.startActivity(i);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == RESULT_LOAD_IMAGE_LOGO && resultCode == RESULT_OK
				&& null != data) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);

			EditText edt = (EditText) findViewById(R.id.txt_logo);
			edt.setText(picturePath);

			cursor.close();
		}
	}
}
